// 语言配置文件
var Languages = {
  // 中文
  zh: {
    // 游戏标题
    title: "2048",
    
    // 游戏介绍
    gameIntro: "合并数字，获得 <strong>2048 方块！</strong>",
    
    // 按钮文本
    newGame: "新游戏",
    keepGoing: "继续游戏",
    tryAgain: "再试一次",
    
    // 游戏说明
    howToPlay: "游戏玩法：使用 <strong>方向键</strong> 移动方块。当两个相同数字的方块相撞时，它们会 <strong>合并成一个！</strong>",
    
    // 注意事项
    note: "注意：这是 2048 的官方版本。你可以通过 <a href=\"http://git.io/2048\">http://git.io/2048</a> 在手机上玩。所有其他应用或网站都是衍生版本或仿制品，使用时请谨慎。",
    
    // 版权信息
    createdBy: "由 <a href=\"http://gabrielecirulli.com\" target=\"_blank\">Gabriele Cirulli</a> 创建。基于 <a href=\"https://itunes.apple.com/us/app/1024!/id823499224\" target=\"_blank\">Veewo Studio 的 1024</a>，概念上类似于 <a href=\"http://asherv.com/threes/\" target=\"_blank\">Asher Vollmer 的 Threes</a>。",
    
    // 分数标签
    score: "分数",
    best: "最高分",
    
    // 游戏消息
    youWin: "你赢了！",
    gameOver: "游戏结束！",
    
    // 设置
    settings: "设置",
    language: "语言",
    chinese: "中文",
    english: "English",
    close: "关闭"
  },
  
  // 英文
  en: {
    // 游戏标题
    title: "2048",
    
    // 游戏介绍
    gameIntro: "Join the numbers and get to the <strong>2048 tile!</strong>",
    
    // 按钮文本
    newGame: "New Game",
    keepGoing: "Keep going",
    tryAgain: "Try again",
    
    // 游戏说明
    howToPlay: "<strong class=\"important\">How to play:</strong> Use your <strong>arrow keys</strong> to move the tiles. When two tiles with the same number touch, they <strong>merge into one!</strong>",
    
    // 注意事项
    note: "<strong class=\"important\">Note:</strong> This site is the official version of 2048. You can play it on your phone via <a href=\"http://git.io/2048\">http://git.io/2048.</a> All other apps or sites are derivatives or fakes, and should be used with caution.",
    
    // 版权信息
    createdBy: "Created by <a href=\"http://gabrielecirulli.com\" target=\"_blank\">Gabriele Cirulli.</a> Based on <a href=\"https://itunes.apple.com/us/app/1024!/id823499224\" target=\"_blank\">1024 by Veewo Studio</a> and conceptually similar to <a href=\"http://asherv.com/threes/\" target=\"_blank\">Threes by Asher Vollmer.</a>",
    
    // 分数标签
    score: "Score",
    best: "Best",
    
    // 游戏消息
    youWin: "You win!",
    gameOver: "Game over!",
    
    // 设置
    settings: "Settings",
    language: "Language",
    chinese: "中文",
    english: "English",
    close: "Close"
  }
};

// 语言管理器
var LanguageManager = {
  currentLang: 'zh', // 默认中文
  
  // 初始化语言
  init: function() {
    // 从本地存储读取语言设置
    var savedLang = localStorage.getItem('2048-language');
    if (savedLang && Languages[savedLang]) {
      this.currentLang = savedLang;
    }
    this.applyLanguage();
  },
  
  // 切换语言
  switchLanguage: function(lang) {
    if (Languages[lang]) {
      this.currentLang = lang;
      localStorage.setItem('2048-language', lang);
      this.applyLanguage();
    }
  },
  
  // 获取当前语言文本
  getText: function(key) {
    return Languages[this.currentLang][key] || Languages['zh'][key] || key;
  },
  
  // 应用语言到界面
  applyLanguage: function() {
    // 更新HTML内容
    document.querySelector('.title').textContent = this.getText('title');
    document.querySelector('.game-intro').innerHTML = this.getText('gameIntro');
    document.querySelector('.restart-button').textContent = this.getText('newGame');
    document.querySelector('.keep-playing-button').textContent = this.getText('keepGoing');
    document.querySelector('.retry-button').textContent = this.getText('tryAgain');
    document.querySelector('.game-explanation').innerHTML = this.getText('howToPlay');
    
    // 更新注意事项
    var noteElement = document.querySelector('.game-explanation').nextElementSibling.nextElementSibling;
    if (noteElement) {
      noteElement.innerHTML = this.getText('note');
    }
    
    // 更新版权信息
    var copyrightElement = noteElement ? noteElement.nextElementSibling.nextElementSibling : null;
    if (copyrightElement) {
      copyrightElement.innerHTML = this.getText('createdBy');
    }
    
    // 更新CSS中的分数标签
    this.updateScoreLabels();
    
    // 更新设置按钮文本
    if (window.settingsButton) {
      window.settingsButton.textContent = this.getText('settings');
    }
    
    // 更新设置弹窗中的文本
    if (window.SettingsManager) {
      window.SettingsManager.updateModalText();
    }
  },
  
  // 更新CSS中的分数标签
  updateScoreLabels: function() {
    // 动态更新CSS中的content属性
    var style = document.getElementById('dynamic-language-style');
    if (!style) {
      style = document.createElement('style');
      style.id = 'dynamic-language-style';
      document.head.appendChild(style);
    }
    
    style.textContent = `
      .score-container:after {
        content: "${this.getText('score')}";
      }
      .best-container:after {
        content: "${this.getText('best')}";
      }
    `;
  }
};
